package com.example.backupgeo.service.impl;

import com.example.backupgeo.common.RoleEnum;
import com.example.backupgeo.entity.User;
import com.example.backupgeo.mapper.UserMapper;
import com.example.backupgeo.service.UserService;
import com.example.backupgeo.util.JWTUtil;
import com.example.backupgeo.util.MD5;
import com.example.backupgeo.util.ResStatus;
import com.example.backupgeo.util.ResultVO;
import com.example.backupgeo.vo.UserVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public ResultVO findAll() {
        List<UserVO> users = userMapper.findAll();
        if(users.isEmpty()){
            return new ResultVO(ResStatus.OK, ResStatus.noData,null);
        } else {
            return new ResultVO(ResStatus.OK, ResStatus.SUCCESS,users);
        }
    }

    @Override
    public ResultVO queryUserInfoByUserId(String userId) {
        if (userId == null) {
            return new ResultVO(ResStatus.OK, ResStatus.messageCannotEmpty,null);
        } else {
            UserVO user = userMapper.queryUserInfoByUserId(userId);
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,user);
        }
    }

    @Override
    public ResultVO addUser(User user) {
        if (user == null) {// 用户不能为空
            return new ResultVO(ResStatus.NO, ResStatus.isNotExist,null);
        } else {
            UserVO userVO = userMapper.queryUserByName(user.getUserName());
            // 用户不存在才需要添加
            if (userVO != null) {
                return new ResultVO(ResStatus.NO,ResStatus.isExisted,null);
            } else {
                try {
                    user.setRole(RoleEnum.ADMIN.name());
                    int res = userMapper.insertUser(user);
                    if (res > 0) {
                        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
                    } else {
                        return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
                    }

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }

    @Override
    public ResultVO login(String name, String password) {
        UserVO user = userMapper.queryUserByName(name);
        //根据账号查询用户信息
        if (user == null){
            // 用户不存在
            return new ResultVO(ResStatus.NO,ResStatus.isNotExist,null);
        }else {
            // 对输入的密码pwd进行加密
            // 使用加密后的密码和用户user中的密码进行匹配
            String md5Pwd = MD5.getMD5(password);

            String token = JWTUtil.createToken(user.getUserId()+"");

            if (ObjectUtils.isEmpty(token)) {
                return new ResultVO(ResStatus.NO, ResStatus.loginError,null);
            } else {

                int result = JWTUtil.verifyToken(token);

                if (result != 0) {
                    return new ResultVO(ResStatus.NO, ResStatus.tokenVerifyFailure,null);
                }
            }
            System.out.println("token is:" + token + "user:"+user.toString());

            if (user.getPassword().equals(md5Pwd)){
                // 验证成功
                user.setToken(token);
                return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,user);
            }else {
                // 密码错误
                return new ResultVO(ResStatus.NO,ResStatus.passwordError,null);
            }
        }
    }

    @Override
    public ResultVO update(User user) {
        try {
            int res = userMapper.update(user);
            if (res > 0) {
                return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
            } else {
                return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
            }
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO,e.getMessage(),null);
        }

    }

    @Override
    public ResultVO deleteUser(String userId) {
        try {
            int res = userMapper.deleteUser(userId);
            if (res > 0) {
                return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
            } else {
                return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
            }
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO,e.getMessage(),null);
        }
    }

}
